/*
 * Copyright 2008 Max Kugland
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.splink.library.observer {
	import org.splink.library.utils.ArrayUtils;		

	/**
	 * Basic <code>ISubject</code> implementation
	 * 
	 * @author Max Kugland
	 */
	public class Subject implements ISubject {
		protected var _observers : Array = new Array();

		public function Subject() {
		}

		/**
		 * @inheritDoc
		 */
		public function subscribe(observer : IObserver) : void {
			_observers.push(observer);
		}

		/**
		 * @inheritDoc
		 */
		public function unsubscribe(observer : IObserver) : void {
			ArrayUtils.remove(_observers, observer);
		}

		/**
		 * @inheritDoc
		 */
		public function notify(data : * = null) : void {
			for each (var observer : IObserver in _observers) {
				observer.update(this, data);
			}
		}

		/**
		 * Unsubscribes all previously registred <code>IObserver</code> objects.
		 * 
		 * @inheritDoc
		 */
		public function finalize() : void {
			_observers = new Array();
		}
	}
}
